/**
 * 
 */
package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ceb.ebank.CebB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @CebB2bManager.java
 * @author Ryan
 * @2013-12-2 上午10:55:23  www.gopay.com.cn Inc.All rights reserved.
 */
public class CebB2bManager implements BankManagerRemoteService {

    /** 分隔符 */
    private static final String SEPARATOR = "~|~";
    
    @Resource(name = "cebB2bBankServiceClient")
    private BankService cebB2bBankServiceClient;
    
    @Resource(name = "cebB2bBankServiceClientHna")
    private BankService cebB2bBankServiceClientHna;
    
    @Resource(name = "cebConfig")
    private Map<String, String> cebConfig;

	
    @Override
    public Object sign(BaseMessage message) throws GopayException {
        CebB2bMessage cebB2bMessage =buildMessage(message);
        cebB2bMessage.setPlainMessage(buildPayPlainStr(cebB2bMessage));
        BankLogUtils.printB2BManagerSignSendLog(message);
        String signVal;
        if(cebB2bMessage.isHna()){
        	signVal = (String) cebB2bBankServiceClientHna.sign(message);
        }else{
        	signVal = (String) cebB2bBankServiceClient.sign(message);
        }
        message.setSignMessage(signVal);
        BankLogUtils.printB2BManagerSignReturnLog(message);
        return message;
    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        // 调用远程接口请求签名数据
        CebB2bMessage message = (CebB2bMessage) msg;
        BankLogUtils.printB2BManagerSignSendLog(message);
        boolean isVerify;
        if(message.isHna()){
        	isVerify = (Boolean) cebB2bBankServiceClientHna.verify(message);
        }else{
        	isVerify = (Boolean) cebB2bBankServiceClient.verify(message);
        }
        BankLogUtils.printB2BManagerVerifyReturnLog(message, isVerify);
        return isVerify;
    }

    @Override
    public Object query(Object obj) throws GopayException {
    	CebB2bMessage cebB2bMessage = (CebB2bMessage) obj;
    	buildQueryMessage(cebB2bMessage);
    	cebB2bMessage.setPlainMessage(buildQueryPlainStr(cebB2bMessage));
		BankLogUtils.printB2BManagerQuerySendLog(cebB2bMessage, cebB2bMessage);
		CebB2bMessage result;
        if(cebB2bMessage.isHna()){
        	result = (CebB2bMessage) cebB2bBankServiceClientHna.query(cebB2bMessage);
        }else{
        	result = (CebB2bMessage) cebB2bBankServiceClient.query(cebB2bMessage);
        }
		result.setOutStlmCd(BankConstants.BANK_LOGO_CEB);
		BankLogUtils.printB2BManagerQueryReturnLog(result);
		return result;
    }

    private CebB2bMessage buildMessage(BaseMessage message) throws GopayException {
        CebB2bMessage cebB2bMessage = (CebB2bMessage) message;        
        cebB2bMessage.setTransId(cebConfig.get("CEB.B2B.TRANSID"));
        cebB2bMessage.setMerchantId(cebB2bMessage.getMerID());       
        cebB2bMessage.setBankUrl(cebB2bMessage.getPayUrl());
        cebB2bMessage.setMerUrl(cebB2bMessage.getPayRuturnUrl());//通知商户URL
        cebB2bMessage.setCurrencyType(cebConfig.get("CEB.CURRENCYTYPE"));
        cebB2bMessage.setMerUrl1(cebB2bMessage.getPayNotifyUrl());// 支付通知商户错误URL1  
        if (StringUtils.isNotBlank(cebB2bMessage.getPaymentAccInfo())) {
            cebB2bMessage.setProductInfo(message.getPaymentAccInfo());
            cebB2bMessage.setMsgExt(message.getPaymentAccInfo());
        }
        return cebB2bMessage;
    }
    
    public String buildPayPlainStr( CebB2bMessage message){
        StringBuffer sb = new StringBuffer(512);
        sb.append("transId=").append(message.getTransId()).append(SEPARATOR)
        .append("merchantId=").append(message.getMerchantId())
        .append(SEPARATOR).append("orderId=")
        .append(message.getOrderId()).append(SEPARATOR)
        .append("transAmt=").append(message.getAmount())
        .append(SEPARATOR).append("transDateTime=")
        .append(message.getOrderDate()).append(SEPARATOR)
        .append("currencyType=").append(message.getCurrencyType())
        .append(SEPARATOR).append("customerName=")
        .append(message.getCustomerName()).append(SEPARATOR)
        .append("productInfo=").append(message.getProductInfo())
        .append(SEPARATOR).append("customerEMail=")
        .append(message.getCustomerEmail()).append(SEPARATOR)
        .append("merURL=").append(message.getMerUrl())
        .append(SEPARATOR).append("merURL1=")
        .append(message.getMerUrl1()).append(SEPARATOR)
        .append("payIp=").append(message.getPayIp()).append(SEPARATOR)
        .append("msgExt=").append(message.getMsgExt());
        return sb.toString();
    }
    
    public String buildQueryPlainStr(CebB2bMessage message){
        StringBuffer paraMap = new StringBuffer();
        paraMap.append("transId=").append(message.getTransId())
        .append(SEPARATOR).append("merchantId=")
        .append(message.getMerchantId()).append(SEPARATOR)
        .append("originalorderId=")
        .append(message.getOriginalorderId()).append(SEPARATOR)
        .append("originalTransDateTime=")
        .append(message.getOriginalTransDateTime()).append(SEPARATOR)
        .append("originalTransAmt=")
        .append(message.getAmount()).append(SEPARATOR)
        .append("merURL=").append(message.getMerUrl());
        return paraMap.toString();
    }
    
    private CebB2bMessage buildQueryMessage(CebB2bMessage cebB2bMessage) {
    	cebB2bMessage.setTransId(cebConfig.get("CEB.B2B.QUERY.TRANSID"));
    	cebB2bMessage.setOriginalTransId(cebConfig.get("CEB.B2B.TRANSID"));
    	cebB2bMessage.setMerchantId(cebB2bMessage.getMerID());
    	cebB2bMessage.setMerUrl(cebConfig.get("CEB.B2B.MERURL"));// 通知商户URL
    	cebB2bMessage.setBankUrl(cebB2bMessage.getQueryUrl());
    	cebB2bMessage.setCurrencyType(cebConfig.get("CEB.CURRENCYTYPE"));
		return cebB2bMessage;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
